define(function (require, exports, module) {

    /**
     * @name Uploadfile
     * @class 提供单文件上传。
     * @requires jquery, swfobject
     * @author 李博龙
     * @version v1.0.0  
	 *
	 * 主要参数：
	 *			url  		上传地址
	 *          filekey		文件name（默认为file）
	 *          types		文件类型（多类型用英文分号或英文逗号分隔）
	 *          minsize		文件最小kb
	 *          maxsize		文件最大kb
	 *
	 * 			其他参数将以key-value形式发送至后台
	 *
	 * 回调方法：
	 *			uploadfileStart		上传开始（返回 file.name,file.size,file.type）
	 *			uploadfileSuccess	上传成功（返回 loader.data）
	 *			uploadfileError		上传失败（返回 "urlError"|"tooSmall"|"tooLarge"|"ioError"|"securityError"） 
     */

    var $ = require('jquery'),
    	$parsejson = require('parsejson'),
        swfobject = require('swfobject'),
        statusCode = {
            ok: 200,
            error: 300,
            timeout: 301
        };
    
    var defaultInitParams = {
    		url: '/static/common/semidata/plugins/uploadfile/1.0.0/uploadfile.swf',
    		width: '100%',
    		height: '100%',
    		version: '10.0.0'
    	},
		defaultVars = {
	    	filekey: 'file'
	    },
		defaultParams = {
	        menu: 'false',
	        quality: 'high',
	        wmode: 'opaque',
	        allowFullScreen: 'true',
	        allowScriptAccess: 'always'
	    },
	    defaultAttrs = {
	        id: 'uploadfile',
	        name: 'uploadfile'
	    }

    /**
     * @name Uploadfile#initUploadfile
     * @function   
     * @desc 播放器初始化方法。
     * @param {Object} options
     */
    var initUploadfile = function (options) {
        var op = $.extend({
                container: '', 	//【必选】
                url: '',			//【必选】
                width: '100%',
                height: '100%',
                vars: null,
                params: null,
                attrs: null,
                success: null,
                error: null,
                start: null,
                isdebug: false
            }, options),
            $container = (typeof op.container=='string'?$(op.container):op.container);
        
        if(semi.utils.isEmpty($container) || semi.utils.isEmpty(op.url)){
        	seajs.log('Error:uploadfile缺少必选参数！');
        	return;
        }
        
        var content = 'uploadfilecontent'+semi.utils.checkId('uploadfilecontent');
        $container.append('<div id="'+content+'"></div>');
        
        var initParams = $.extend(defaultInitParams, {width:op.width, height:op.height});
        op.vars = $.extend(defaultVars, op.vars, {url:op.url});
        op.params = $.extend(defaultParams, op.params);
        op.attrs = $.extend(defaultAttrs, op.attrs);
        
        var _checkId = semi.utils.checkId(op.attrs.id);
        if(_checkId!=0){
        	op.attrs = {
        	    id: op.attrs.id+_checkId,
        	    name: op.attrs.name+_checkId
        	}
        }
        
        window.uploadfileStart = function(name, size, type){
        	if(!semi.utils.isEmpty(op.start) && $.isFunction(op.start)){
        		op.start.call(this, name, size, type);
        	}
        }
        
        window.uploadfileSuccess = function(data){
        	var status;
            data = $parsejson(data);
            seajs.log('Uploadfile JS Say:上传成功！')
            seajs.log(data)
            status = _checkStatusCode(data);
            if(status == statusCode.error || status == statusCode.timeout) {
            	uploadfileError(data);
            }else{
                if(!semi.utils.isEmpty(op.success) && $.isFunction(op.success)){
                    op.success.call(this, data);
                }
            }
        }
        
        window.uploadfileError = function(data){
        	seajs.log('Uploadfile JS Say:上传失败！')
            seajs.log(data)
        	if(!semi.utils.isEmpty(op.error) && $.isFunction(op.error)){
        		op.error.call(this, data);
        	}
        }
        
        window.console_log = function(value){
    		if(op.isdebug)seajs.log(value);
    	}
        
        swfobject.embedSWF(initParams.url, content, initParams.width, initParams.height, initParams.version, swfobject.installUrl, op.vars, op.params, op.attrs);
    }

    /**
     * @name Uploadfile#_checkStatusCode
     * @desc 检查返回值status。
     * @event
     * @param {JSON} json
     * 
     */
    var _checkStatusCode = function(json) {
        if(semi.utils.isEmpty(json) || semi.utils.isEmpty(json.statusCode)) {
            return statusCode.ok;
        }
        if(json.statusCode == statusCode.error) {
            return statusCode.error;
        }else if(json.statusCode == statusCode.timeout) {
            return statusCode.timeout;
        }
        return statusCode.ok;
    }
    
    return {
        init: initUploadfile
    }
});